<?php
/**
 * Show the logs
 *
 * @author University of London Computer Centre
 * @version 1.1.0
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 * @global ArchivePressLog $ArchivePressLog
 */

// globals
global $ArchivePress;
global $ArchivePressLog;

// actions URL
$responder = get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/responder.php&amp;back=logs';

// get info to apply the filters
// STATUS
$status = "all";
if(isset($_GET["log_status"])){
    $status = attribute_escape($_GET["log_status"]);
}

// SEARCH FIELD
$search = "";
if(isset($_GET["s"])){
    $search = stripslashes($_GET["s"]);
}


// CHECK FOR MESSAGES
$message = "";
// delete
if(array_key_exists("deletelog", $_GET)){
    if((int)$_GET["deletelog"] == 1){
        // SINGLE ACTION
        $message = __("1 Log Deleted", ARCHIVEPRESS);
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Logs Deleted",ARCHIVEPRESS), (int)$_GET["deletelog"]);
    }
}

// logon
if(array_key_exists("logon", $_GET)){
    $message = __("ArchivePress Log System ON", ARCHIVEPRESS);
}

// logoff
if(array_key_exists("logoff", $_GET)){
    $message = __("ArchivePress Log System OFF", ARCHIVEPRESS);
}

// logempty
if(array_key_exists("logempty", $_GET)){
    $message = __("All the logs have been deleted from the database", ARCHIVEPRESS);
}


if($message != ""){
    $message = '<div class="updated fade below-h2" id="message">
                    <p>
                        '.$message.'<br />
                    </p>
                </div>';
}


// ON/OFF logging
if(get_option(WP_ARCHIVEPRESS_PREFIX . "defLogStatus") == ArchivePressLog::LOG_ON) {
    $logonoff = '<a class="button add-new-h2" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=logoff', WP_ARCHIVEPRESS_PREFIX . 'logoff').'">'.__("Turn Log Off", ARCHIVEPRESS).'</a>';
} else {
    $logonoff = '<a class="button add-new-h2" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=logon', WP_ARCHIVEPRESS_PREFIX . 'logon').'">'.__("Turn Log On", ARCHIVEPRESS).'</a>';
}

echo '<div class="wrap">
        <div class="icon32" id="icon-archivepress"><br /></div>
        <h2>'.__("ArchivePress Logs", ARCHIVEPRESS).'
            '.$logonoff.'
            <a class="button add-new-h2" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=emptylog', WP_ARCHIVEPRESS_PREFIX . 'emptylog').'">'.__("Empty Log Table", ARCHIVEPRESS).'</a>
        </h2>

        '.$message.'

        <form method="get" action="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=show_logs', WP_ARCHIVEPRESS_PREFIX . 'show_logs').'" id="posts-filter">
        <input type="hidden" name="page" value="archivepress/pages/responder.php" />
        <input type="hidden" name="'.WP_ARCHIVEPRESS_PREFIX.'action" value="show_logs" />
        <input type="hidden" name="back" value="logs" />
        <input type="hidden" name="_wpnonce" value="'.wp_create_nonce(WP_ARCHIVEPRESS_PREFIX . 'show_logs').'" />';


// total, basic and debug logs
if($status == "all"){
    echo '
        <ul class="subsubsub">
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getTotalLogs().')</span></a> |</li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=basic">'.__("Basic", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getBasicLogs().')</span></a></li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=debug">'.__("Debug", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getDebugLogs().')</span></a></li>
        </ul>';
} else if($status == "basic"){
    echo '
        <ul class="subsubsub">
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getTotalLogs().')</span></a> |</li>
            <li><a class="current"  href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=basic">'.__("Basic", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getBasicLogs().')</span></a></li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=debug">'.__("Debug", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getDebugLogs().')</span></a></li>
        </ul>';
} else if($status == "debug"){
    echo '
        <ul class="subsubsub">
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getTotalLogs().')</span></a> |</li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=basic">'.__("Basic", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getBasicLogs().')</span></a></li>
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=debug">'.__("Debug", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getDebugLogs().')</span></a></li>
        </ul>';
} else {
    echo '
        <ul class="subsubsub">
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getTotalLogs().')</span></a> |</li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=basic">'.__("Basic", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getBasicLogs().')</span></a></li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/logs.php&amp;log_status=debug">'.__("Debug", ARCHIVEPRESS).' <span class="count">('.$ArchivePressLog->getDebugLogs().')</span></a></li>
        </ul>';
}

// PAGINATION
$paging = "";
if(isset($_GET['paging'])){
    $paging = attribute_escape($_GET['paging']);
}
$items = count($ArchivePressLog->getFilteredLogs($status, $search));

$p = new pagination;
$p->items($items);
$p->limit(15); // Limit entries per page
$p->target(str_replace("&","&amp;",$_SERVER['REQUEST_URI']));
$p->currentPage($paging); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(2); //No. of page away from the current page

if($paging == "") {
    $p->page = 1;
} else {
    $p->page = $paging;
}

$p->prevT = "";
$p->nextT = "";
$p->displaying = __("Displaying", ARCHIVEPRESS);
$p->of = __("of", ARCHIVEPRESS);

// limit for query
$limit = " LIMIT " . ($p->page - 1) * $p->limit  . ", " . $p->limit;

echo'
        <p class="search-box">
            <label for="post-search-input" class="screen-reader-text">'.__("Search Logs", ARCHIVEPRESS).':</label>
            <input type="text" value="'.$ArchivePress->cleanOutput($search).'" name="s" id="post-search-input" />
            <input type="submit" class="button" value="'.__("Search Logs", ARCHIVEPRESS).'" />
        </p>

        <div class="tablenav">

            <div class="alignleft actions">
                <select name="action">
                    <option selected="selected" value="-1">'.__("Bulk Actions", ARCHIVEPRESS).'</option>
                    <option value="deletelog">'.__("Delete", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary action" id="doaction" name="doaction" value="Apply" />
            </div>

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="clear"></div>
        </div>

        <div class="clear"></div>

        <table cellspacing="0" class="widefat post fixed">
            <thead>
                <tr>
                    <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_message" scope="col">'.__("Message", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_time" id="time" scope="col">'.__("Time", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_level" id="level" scope="col">'.__("Level", ARCHIVEPRESS).'</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <th style="" class="manage-column column-cb check-column" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_message" scope="col">'.__("Message", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_time" scope="col">'.__("Time", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_level" scope="col">'.__("Level", ARCHIVEPRESS).'</th>
                </tr>
            </tfoot>

            <tbody>';

// get logs
$logs = $ArchivePressLog->getFilteredLogs($status, $search, $limit);


// print feeds
if(count($logs) > 0){
    foreach($logs as $log){

            echo'
                    <tr valign="top" class="alternate iedit" id="log'.$log->id.'">
                        <th class="check-column" scope="row"><input type="checkbox" value="'.$log->id.'" name="log[]" /></th>
                        <td class="column-arpr_message">'.$ArchivePress->cleanOutput($log->message).'
                            <div class="row-actions">
                                <span class="trash"><a title="'.__("Delete this log", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=deletelog&amp;log_id='.$log->id, WP_ARCHIVEPRESS_PREFIX . 'deletelog_' . $log->id).'">'.__("Delete", ARCHIVEPRESS).' </a></span>
                            </div>
                        </td>
                        <td class="column-arpr_time">'.mysql2date('F jS, Y @ H:i',$ArchivePress->cleanOutput($log->log_date)).'</td>
                        <td class="column-arpr_level">'.$ArchivePress->cleanOutput($log->level).'</td>
                    </tr>';
    }
} else {
    echo '<tr valign="top" class="alternate iedit" id="feed-0"><td colspan="4">'.__("No Logs found", ARCHIVEPRESS).'</td></tr>';
}

echo'
            </tbody>
        </table>
        <div class="tablenav">

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="alignleft actions">
                <select name="action2">
                    <option selected="selected" value="-1">'.__("Bulk Actions", ARCHIVEPRESS).'</option>
                    <option value="deletelog">'.__("Delete", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary action" id="doaction2" name="doaction2" value="Apply" />
                <br class="clear" />
            </div>
            <br class="clear" />
        </div>
    </form>
    </div>';

?>